Repository navigation
fix(codex-auth): handle nested refresh errors and prefer access_token expiry - #3286
Open
littlemoonly wants to merge 1 commit into
Open
littlemoonly wants to merge 1 commit into
littlemoonly wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
修复 Codex OAuth Token 刷新流程中的两个问题:
兼容嵌套字典格式的刷新错误响应,避免
TypeError: unhashable type: 'dict'deeptutor/services/codex_auth/oauth.py原先直接判断rejection.get("error") in {"invalid_grant", "invalid_token"}。当 OpenAI 鉴权接口在 400/401 响应中返回嵌套对象(例如{"error": {"type": "invalid_request_error", "code": "refresh_token_reused", ...}})时,对dict做集合查找会直接抛出TypeError,导致未能标记token_refresh_rejected/ 引导用户重新登录,而是每次请求重复报错Codex request failed。现新增
_is_refresh_rejection(),安全提取字符串或嵌套error.code/error.type(覆盖invalid_grant、invalid_token、refresh_token_expired、refresh_token_reused、refresh_token_invalidated),非拒绝类或不可哈希错误不再崩溃,统一按token_refresh_failed处理。优先使用
access_token的过期时间,避免每小时无效刷新deeptutor/services/codex_auth/service.py的_credentials_from_payload()原先按(id_token, access_token)顺序提取expires_at,会优先取到有效期仅 1 小时的id_token.exp,覆盖掉有效期约 10 天的access_token.exp,导致实际有效的access_token每小时都被强制触发refresh_token。现调整为优先取
access_token的 JWTexp(其次expires_in,最后回退id_token.exp),account_id解析顺序保持不变。复现步骤
问题 1(刷新错误触发
TypeError):refresh_token在服务端失效/被复用。expires_at - now <= 300)时发起任意 Codex 对话请求。/oauth/token返回 HTTP 400/401 且响应体为{"error": {"code": "refresh_token_reused", ...}}。oauth.py::_token_request抛出TypeError: unhashable type: 'dict',未触发mark_authentication_rejected(),前端仅收到泛化的Codex request failed. Please try again.,后续请求持续重复报错。问题 2(误用
id_token.exp导致每小时刷新):credentials.v1.json。id_token(exp为 1 小时后)与access_token(exp为 10 天后),可见存储的expires_at被写成了id_token.exp。access_token仍有约 10 天有效期,仍会强制走_refresh_credentials()。Related Issues
Module(s) Affected
servicestestsChecklist
dev(notmain— see the base-branch selector above).pre-commit run --all-filesand fixed any issues.Additional Notes
测试与检查结果:
pytest tests/services/codex_auth/ tests/services/llm/test_openai_codex_oauth_provider.py tests/api/test_codex_oauth_callback.py tests/api/test_codex_oauth_scope.py:340 passedpytest tests/multi_user/test_personal_codex_models.py:10 passedruff check&ruff format --check(改动文件):全部通过